import 'package:flutter/material.dart';

import 'data_base.dart';
import 'package:sqflite/sqflite.dart';

class NovelTable {
  static NovelTable _novelTable;

  static NovelTable getInstance() {
    if (_novelTable == null) {
      _novelTable = NovelTable();
    }
    return _novelTable;
  }

  Future<int> insertInfo(NovelTableInfo info) async {
    String sql =
        "INSERT INTO Novel(id,authorId,authorName,bookName,bid,cid,bookCover,className,bookInfo) "
        "VALUES (?,?,?,?,?,?,?,?,?)";
    DataBaseUtil.getInstance().getDataBase().transaction((trans) async {
      int t = 0;
      getInfoByBid(info.bid).then((value) async {
        if (value == null) {
          t = await trans.rawInsert(sql,[null,info.authorId,info.authorName,info.bookName,info.bid,info.cid,info.bookCover,info.className,info.bookInfo]);
        }
      });
      return t;
    });
    return 0;
  }

  Future<int> insertBook(
      int id,
      String authorId,
      String authorName,
      String bookName,
      String bid,
      String cid,
      String bookCover,
      String className,
      String bookInfo) async {
    String sql =
        "INSERT INTO Novel(id,authorId,authorName,bookName,bid,cid,bookCover,className,bookInfo) "
        "VALUES (?,?,?,?,?,?,?,?,?)";
    DataBaseUtil.getInstance().getDataBase().transaction((trans) async {
      int t = 0;
      getInfoByBid(bid).then((value) async {
        if (value == null) {
          t = await trans.rawInsert(sql, [
            null,
            authorId,
            authorName,
            bookName,
            bid,
            cid,
            bookCover,
            className,
            bookInfo
          ]);
        }
      });
      return t;
    });
    return 0;
  }

  void rowUpdata(String bid, String cid, Function fun) async {
    String sql = "UPDATE Novel SET cid = ? WHERE bid = ?";
    Database database = DataBaseUtil.getInstance().getDataBase();
    var t = await database.transaction((trans) async {
      int t = await trans.rawUpdate(sql, [cid, bid]);
      fun(t);
    });
  }

  Future<NovelTableInfo> getInfoByBid(String bid) async {
    var info = await DataBaseUtil.getInstance()
        .getDataBase()
        .rawQuery("SELECT * FROM Novel WHERE bid=$bid");
    print(info);
    if (info == null || info.length == 0) {
      return null;
    } else {
      return NovelTableInfo.get(info[0]);
    }
  }

  Future<List<NovelTableInfo>> getAllInfo() async {
    var info = await DataBaseUtil.getInstance().getDataBase().query("Novel");
    if (info == null) {
      return null;
    } else {
      List<NovelTableInfo> novels = [];
      for (var item in info) {
        novels.add(NovelTableInfo.get(item));
      }
      return novels;
    }
  }

  void deleteById(int id, Function fun) async {
    var t = await DataBaseUtil.getInstance()
        .getDataBase()
        .delete("Novel", where: "id=?", whereArgs: [id]);
    fun(t);
  }

  void deleteByIds(List<int> ids, Function fun) {
    for (var t in ids) {
      deleteById(t, (value) {
        print("delete $value");
      });
    }
    fun();
  }
}

class NovelTableInfo {
  int id;
  String authorId;
  String authorName;
  String bookName;
  String bid;
  String cid;
  String bookCover;
  String className;
  String bookInfo;

  NovelTableInfo(
      {this.id,
      this.authorId,
      this.authorName,
      this.bookName,
      this.bid,
      this.cid,
      this.bookCover,
      this.className,
      this.bookInfo});

  static NovelTableInfo get(Map<String, dynamic> map) {
    return NovelTableInfo(
      id: map["id"],
      authorId: map["authorId"],
      authorName: map["authorName"],
      bookName: map["bookName"],
      bid: map["bid"],
      cid: map["cid"],
      bookCover: map["bookCover"],
      bookInfo: map["bookInfo"],
      className: map["className"],
    );
  }
}
